package com.cl.ks.service;


import com.cl.schedule.schedule.CronTaskRegistrar;
import com.cl.schedule.service.ScheduleService;
import com.cl.ks.entity.KsFlowTask;
import com.cl.ks.repository.KsFlowTaskRepository;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Optional;

import static com.cl.ks.constant.QTableConstant.*;

@Service
public class KsFlowTaskService extends ScheduleService<KsFlowTask> {
    private final JPAQueryFactory queryFactory;
    protected KsFlowTaskService(KsFlowTaskRepository ksFlowTaskRepository, CronTaskRegistrar<KsFlowTask> cronTaskRegistrar, JPAQueryFactory queryFactory, SparkNodeService sparkNodeService) {
        super(ksFlowTaskRepository, cronTaskRegistrar);
        this.queryFactory = queryFactory;
        this.sparkNodeService = sparkNodeService;
    }

    private final SparkNodeService sparkNodeService;

    @Override
    @Transactional
    public void deleteById(Integer id) {
        Optional<KsFlowTask> ksFlowTask = findById(id);
        if (ksFlowTask.isPresent()) {
            queryFactory.delete(qKsFlowGraphLog).where(qKsFlowGraphLog.ksFlowTaskId.eq(id)).execute();
            queryFactory.delete(qKsFlowGraphRecord).where(qKsFlowGraphRecord.ksFlowTaskId.eq(id)).execute();
            queryFactory.delete(qKsFlowTask).where(qKsFlowTask.id.eq(id)).execute();
            removeCronTask(id);
        }

    }
//    @Override
//    public Page<KsFlowTask> page(KsFlowTask entity, String auth) {
//        Page<KsFlowTask> all = repository.findAll(buildSpecification(entity).and(buildPowerSpecification(auth)), PageRequest.of(entity.getPageNumber() - 1, entity.getPageSize()));
//        all.getContent().forEach(i->{
//            i.setProgress(KsFlowGraphHandler.getProgress(i.getId(),0));
//        });
//        return all;
//    }
}
